﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AMT.IdentityModel.OAuth.Tests
{
	class TestParameters
	{
		#region Public properties
		/// <summary>
		/// Base URI for Security Token Service (STS) endpoints
		/// </summary>
		public Uri STSBaseUri { get; set; }

		/// <summary>
		/// Issuer's symmetric signing key
		/// </summary>
		public string IssuerSymmetricKey { get; set; }

		/// <summary>
		/// Scope (URI) for token request
		/// </summary>
		public string Scope { get; set; }

		/// <summary>
		/// Identity name for token request
		/// </summary>
		public string IdentityName { get; set; }

		/// <summary>
		/// Identity password for token request
		/// </summary>
		public string IdentityPassword {get;set;}

		/// <summary>
		/// Identity symmetric signing key for token request
		/// </summary>
		public string IdentitySymmetricKey {get;set;}

		/// <summary>
		/// OAuth 2.0 protocol endpoint relative to STSBaseUri
		/// </summary>
		public string OAuth20EndpointPath { get; set; }

		/// <summary>
		/// WRAP protocol endpoint relative to STSBaseUri
		/// </summary>
		public string WRAPEndpointPath { get; set; }
		#endregion

		#region Public methods
		public static TestParameters LoadFromConfig()
		{
			TestParameters t = new TestParameters();
			Properties.Settings s = Properties.Settings.Default;

			t.STSBaseUri = new Uri(s.STSBaseUri);
			t.OAuth20EndpointPath = s.OAuth20EndpointPath;
			t.WRAPEndpointPath = s.WRAPEndpointPath;
			t.IssuerSymmetricKey = s.IssuerSymmetricKey;
			t.Scope = s.Scope;
			t.IdentityName= s.IdentityName;
			t.IdentityPassword = s.IdentityPassword;
			t.IdentitySymmetricKey = s.IdentitySymmetricKey;

			return t;
		}

		// For Symmetric key claims
		public static string GetParamInfoForOutput(AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserSymmetricKeyClaimToken> wh)
		{
			return BuildParamInfoString(wh.Scope, wh.STSUri.AbsoluteUri, wh.ClaimToken.UserName, "Symmetric Key");
		}

		// For user password claims
		public static string GetParamInfoForOutput(AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserPasswordClaimToken> wh)
		{
			return BuildParamInfoString(wh.Scope, wh.STSUri.AbsoluteUri, wh.ClaimToken.UserName, "Password");
		}
		#endregion

		// For SAML 2.0
		public static string GetParamInfoForOutput(AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SAML20UserPasswordClaimToken> wh)
		{
			// TODO: retrieve user name from Saml assertions
			return BuildParamInfoString(wh.Scope, wh.STSUri.AbsoluteUri, "### TODO: get user name from saml token ###", "Password");
		}

		private static string BuildParamInfoString(string scope, string stsEndpoint, string userName, string credentialType)
		{
			return string.Format("Scope: {0}; STSBaseUri: {1}; User: {2}; Credentials Type: {3}", scope, stsEndpoint, userName, credentialType);
		}
	}
}
